import http from '@/static/js/request';
import util from '@/static/js/util';
import { defineStore } from 'pinia'
import { pswEncode } from '@/static/js/config';

interface AppSate {
    userInfo:Record<string, any>,
    accessToken: string | null,
    currentCity: Record<string, any>,
    currentSite: Record<string, any>,
    showMsgBox: Boolean,
}

export const useAppStore = defineStore('appStore',{
    state: (): AppSate => ({
        userInfo: {},
        accessToken: '',
        currentCity: {"code":"440800","name":"湛江市"},
        currentSite: {},
        showMsgBox: false,
    }),
    getters: {
        getShowMsgBox: state => state.showMsgBox,
        getCurrentCity: state => state.currentCity,
    },
    actions: {
        // 微信登录
        wxLogin(){
            return new Promise(async (resolve,reject) => {
                const { code }: any = await uni.login({
                    provider: 'weixin'
                });
                let params = {
                  mobile: `APP-MINI@${code}`,
                  code: code,
                  grant_type: 'mobile',
                  scope: 'server',
                };
                http('post', '/auth/oauth2/token'+ util.serialize(params), {}, (data,msg) => {
                    this.accessToken = data.access_token;
                    uni.setStorageSync('accessToken', data.access_token);
                    this.getUserInfo();
                    resolve(data);
                },(msg) => {
                    reject(msg);
                }, {
                    header:{
                        Authorization : 'Basic ' + util.encryptionBase64(pswEncode)
                    }
                },'all');
            })            
        },
        // 手机验证码登录
        phoneLogin(obj){
            return new Promise(async (resolve,reject) => {
                let params = {
                  mobile: `APP-SMS@${obj.phone}`,
                  code: obj.code,
                  grant_type: 'mobile',
                  scope: 'server',
                };
                http('post', '/auth/oauth2/token'+ util.serialize(params), {}, (data,msg) => {
                    this.accessToken = data.access_token;
                    uni.setStorageSync('accessToken', data.access_token);
                    this.getUserInfo();
                    resolve(data);
                },(msg) => {
                    reject(msg);
                }, {
                    header:{
                        Authorization : 'Basic ' + util.encryptionBase64(pswEncode)
                    }
                },'all');
            });
        },
        // 获取用户信息
        getUserInfo(){
            return new Promise((resolve, reject) => {
                http('get', '/app/appuser/info', {}, (data,msg) => {
                    this.userInfo = data.appUser;
                    uni.setStorageSync('userInfo', JSON.stringify(data.appUser));
                    resolve(data.appUser);
                },msg => {
                    reject(msg);
                });
            });
        },
        // 修改用户信息
        editUserInfo(params){
            return new Promise((resolve, reject) => {
                http('put', '/app/appuser/edit', params, (data,msg) => {
                    this.userInfo = data.appUser;
                    resolve(data.appUser);
                },msg => {
                    reject(msg);
                });
            });
        },
        // 微信支付
        wechatPay(params){
            return new Promise((resolve, reject) => {
                http('post', '/admin/oms/pay/payment', params, (data,msg) => {
                    resolve(data);
                },msg => {
                    reject(msg);
                });
            })
        }
    }
})
